/**
 * 
 */
package com.geezdata.cps.ttk.query.query.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.ttk.common.request.QueryBillListRequest;
import com.geezdata.cps.ttk.common.response.QueryBillListResult;
import com.geezdata.cps.ttk.common.vo.BillVO;
import com.geezdata.cps.ttk.common.vo.PinduoduoDDKGoodsDetailResVO;
import com.geezdata.cps.ttk.common.vo.SharerResVO;
import com.geezdata.cps.ttk.common.vo.TaskResVO;
import com.geezdata.cps.ttk.domain.domain.Bill;
import com.geezdata.cps.ttk.domain.domain.Goods;
import com.geezdata.cps.ttk.domain.domain.Sharer;
import com.geezdata.cps.ttk.domain.domain.Task;
import com.geezdata.cps.ttk.mapper.mapper.BillMapper;
import com.geezdata.cps.ttk.query.query.BillService;
import com.geezdata.cps.ttk.query.query.GoodsService;
import com.geezdata.cps.ttk.query.query.SharerService;
import com.geezdata.cps.ttk.query.query.TaskService;
import com.google.common.collect.Maps;

/**
 * @author sszheng
 * @date 创建时间：2018年11月6日 下午3:16:29
 *
 */
@Service
public class BillServiceImpl implements BillService {
	@Autowired
	private BillMapper billMapper;
	@Autowired
	private SharerService sharerService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private GoodsService goodsService;

	@Override
	public QueryBillListResult getBillList(QueryBillListRequest request) {
		QueryBillListResult result = new QueryBillListResult();
		
		Sharer sharer = sharerService.getSharerByOpenId(request.getOpenId());
		SharerResVO sharerResVO = BeanUtil.convert(sharer, SharerResVO.class);
		Map<String, Object> params = Maps.newHashMap();
		params.put("openId", request.getOpenId());
		params.put("billStatus", request.getBillStatus());
		params.put("orderStatus", request.getOrderStatus());
		params.put("billType", request.getBillType());
		List<Bill> billList = billMapper.selectBatch(params );
		List<BillVO> list = BeanUtil.convertList(billList, BillVO.class);
		list.stream().forEach(bill->setExt(bill));
		result.setList(list);
		result.setSharerResVO(sharerResVO);
		return result;
	}

	private void setExt(BillVO bill) {
		Task task = taskService.getDetail(bill.getTaskId());
		TaskResVO taskResVO = BeanUtil.convert(task, TaskResVO.class);
		Sharer header = sharerService.getSharerByOpenId(task.getOpenId());
		SharerResVO headerVO = BeanUtil.convert(header, SharerResVO.class);
		taskResVO.setHeader(headerVO);
		
		Goods goods = goodsService.getGoodsByPddGoodsId(task.getGoodsId());
		PinduoduoDDKGoodsDetailResVO goodsResVO = BeanUtil.convert(goods, PinduoduoDDKGoodsDetailResVO.class);
		taskResVO.setGoods(goodsResVO);
		
		bill.setTask(taskResVO);
	}
}
